/**
 * Copyright (C), 2015-2020, XXX有限公司
 * FileName: P10cutString
 * Author:   wanglm
 * Date:     2020/06/01 11:49
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.app.bhca.cutP10Tool;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author wanglm
 * @create 2020/06/01
 * @since 1.0.0
 */
public class P10cutString extends JFrame {
    public static void main(String[] args) {

        P10cutString p10cutString = new P10cutString();
        p10cutString.run();

    }

    public void run(){
        //创建JLabel设置图标  180x50
        ImageIcon icon = new ImageIcon("./file/easyicon_net_16.ico");
        JLabel label1 = new JLabel(icon);
        label1.setBounds(160, 35, 180, 50);

        final  JTextArea text_old = new  JTextArea();
        final  JTextArea text_new = new  JTextArea("",10,80);

        Rectangle rectangle_old = new Rectangle(10,10,330,420);
        text_old.setBounds(rectangle_old);
        text_old.setLineWrap(true);

        Rectangle rectangle_new = new Rectangle(440,10,330,420);
        text_new.setBounds(rectangle_new);
        text_new.setLineWrap(true);
        text_new.setEditable(false);

        //设置输入的字体
        text_old.setFont(new Font("微软雅黑", Font.CENTER_BASELINE, 13));
        text_new.setFont(new Font("微软雅黑", Font.CENTER_BASELINE, 13));

        //创建登录和取消的JButton
        final JButton selectFileBtn = new JButton("选择文件");
        final JButton changeBtn = new JButton("转换");
        final JButton copyBtn = new JButton("复制");

        //设置JButton的大小
        Rectangle rectangle_selectFileBtn = new Rectangle(350,80,80,30);
        selectFileBtn.setBounds(rectangle_selectFileBtn);

        Rectangle rectangle_changeBtn = new Rectangle(350,120,80,30);
        changeBtn.setBounds(rectangle_changeBtn);

        Rectangle rectangle_copyBtn = new Rectangle(350,180,80,30);
        copyBtn.setBounds(rectangle_copyBtn);


        //设置布局管理器
        Container container = this.getContentPane();

        //将组件加入到布局管理器
        container.add(label1);
        container.add(text_old);
        container.add(text_new);
        container.add(selectFileBtn);
        container.add(changeBtn);
        container.add(copyBtn);


        /**
         * 对按钮附加功能 采用监听事件完成
         * */
        selectFileBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser jFileChooser = new JFileChooser("c:\\test");
                jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                jFileChooser.showDialog(new JLabel(),"选择P10文件");

                File file=jFileChooser.getSelectedFile();
                if(file.isDirectory()){
                    System.out.println("文件夹:"+file.getAbsolutePath());
                    return;
                }else if(file.isFile()){
                    System.out.println("文件:"+file.getAbsolutePath());
//                    System.out.println(jFileChooser.getSelectedFile().getName());

                    String P10Str = readFile(file.getAbsolutePath());
                    text_old.setText(P10Str);
                }

            }
        });

        changeBtn.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                String oldStr = text_old.getText();
                cutString cut = new cutString();
                String newStr = cut.cStr(oldStr);
                text_new.setText(newStr);
            }

        });

        copyBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String copyContent = text_new.getText();
                if(copyContent.trim().isEmpty()){
                    JOptionPane.showMessageDialog(
                            null,
                            "文本内容为空！",
                            "空值",
                            JOptionPane.WARNING_MESSAGE
                    );
                }else {
                    Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
                    Transferable tText = new StringSelection(copyContent);
                    clip.setContents(tText, null);
                    JOptionPane.showMessageDialog(
                            null,
                            "已复制到剪切板！",
                            "复制文本",
                            JOptionPane.INFORMATION_MESSAGE
                    );
                }
            }
        });


        //设置窗体的Logo
        Image iconLogo=Toolkit.getDefaultToolkit().createImage("./file/easyicon_net_16.ico");
        this.setIconImage(iconLogo);
        //设置窗体的属性
        this.setLayout(null);//设置空布局管理器
        this.setVisible(true);//设置窗体可见
        this.setTitle("P10文件转换");//设置窗体标题
        this.setSize(800, 500);//设置窗体的高、宽
        this.setLocationRelativeTo(null);//设置居中显示
        //设置Jframe窗体关闭时 程序结束
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }



    public static String readFile(String strFile){
        String readContent = "";
        try{
            InputStream is = new FileInputStream(strFile);
            int iAvail = is.available();
            byte[] bytes = new byte[iAvail];
            is.read(bytes);
            readContent = new String(bytes);
            is.close();
        }catch(Exception e){
            e.printStackTrace();
        }

        return readContent;
    }

}
